<template>
  <commonPage :showHeader="false" :showMaxPage="true">
    <scroller slot="page_body" style="height: 100%" ref="my_scroller" refresh-layer-color="#4b8bf4" loading-layer-color="#ec4949" class="page_body">
      <div style="margin-top:3rem">
        <input type="text" v-model="username" placeholder="用户名">
        <input type="pasword" v-model="password" placeholder="密码">
        <input type="button" @click="login" value="登录">
      </div>
      <input type="button" @click="vibrate" value="震动">
    </scroller>
    <div slot="page_footer" />
  </commonPage>
</template>

<style lang="stylus" scoped  ></style>
<script>
import { getCode ,vibrate} from '../config/dd'
import commonPage from "../components/common/commonPage";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
      tips: "",
      stat: false,
      login_show: false
    };
  },
  components: {
    commonPage,
  },
  async created() {
    // var param = { code: '26acc291c1bb3da2bbdad6a93fc42d63' };

    //  this.$axios.post("h5appapi/ding/login", param).then(res => {
    //   console.log(res)
    //   // alert();
    //   if (res.code != 20000) {
    //     console.log(res)
    //     this.$toast({
    //       position: 'bottom',
    //       message: res.message
    //     });
    //     return false;
    //   }
    // })

    await getCode((code) => {
      this.code = code
      if (this.code != null && this.code != '') {
        // 获取到code后执行获取用户信息并登录操作
        this.$axios.post("h5appapi/ding/login", { code: this.code }).then(res => {
          if (res.code == 20000) {
            console.log(res.data)

            // localStorage.setItem('avatar', res.data.user.avatar)
            this.$toast({
              position: 'bottom',
              message: res.message
            });
            // setTimeout(function () {
            //   let url = !this.$route.query.redirect ? '/' : this.$route.query.redirect
            //   this.$router.replace({ 'path': url });
            // }.bind(this), 1500);
          } else if (res.code != 20000) {
            console.log(res)

            this.$toast({
              position: 'bottom',
              message: res.message
            });
            return false;
          }
        }).catch(error => {
          console.log(error)
        });
      }
    })
  },
  methods: {
    vibrate() {
      vibrate()

    },
    login: function () {
      if (!this.username) {
        this.$toast({
          position: 'bottom',
          message: '请输入用户名'
        });
        return
      } else if (!this.password) {
        this.$toast({
          position: 'bottom',
          message: '请输入密码'
        });
        return
      } else {
        let param = {
          username: this.username,
          password: this.password
        };
        this.$axios.post("h5appapi/user/login", param).then(res => {
          if (res.code == 20000) {
            console.log(res.data)
            localStorage.setItem('token', res.data.token);
            localStorage.setItem('nickname', res.data.user.realname)
            localStorage.setItem('username', res.data.user.username)
            // localStorage.setItem('avatar', res.data.user.avatar)
            this.$toast({
              position: 'bottom',
              message: res.message
            });
            setTimeout(function () {
              let url = !this.$route.query.redirect ? '/' : this.$route.query.redirect
              this.$router.replace({ 'path': url });
            }.bind(this), 1500);
          } else if (res.code != 20000) {
            this.$toast({
              position: 'bottom',
              message: res.message
            });
            return false;
          }
        }).catch(error => {
          console.log(error)
        });
      }
      setTimeout(
        function () {
          this.stat = false;
        }.bind(this),
        1000
      );
    }
  }
};
</script>